package main

import "fmt"

type Stu struct {
	age  int
	name string
}

func t1() {
	fmt.Println("11111111111")
}

func t2() {
	fmt.Println("22222222222222222222")
}

func defer1() {
	defer fmt.Println("0000")
	fmt.Println("defer1")
	return
}

// 多个defer  - FILO
func defer2() {
	defer fmt.Println("222")
	defer fmt.Println("333")
	fmt.Println("defer2")
	return
}

// 测试参数传递
func defer3() {
	i := 3
	defer fmt.Printf("i: %d\n", i)
	i = 4
	defer fmt.Printf("i: %d\n", i)
	fmt.Println("defer3")
}

// 调用defer 的时候已经压栈，修改不影响
func defer4() {
	u := Stu{age: 10, name: "cosco"}
	defer fmt.Println(u)
	u.age = 20
	defer fmt.Println(u)
	fmt.Println("defer4")
}

func defer5() {
	u := Stu{age: 30, name: "cosco"}
	var pu *Stu
	pu = &u
	defer fmt.Println(*pu)
	pu.age = 40
	defer fmt.Println(*pu)
}

func main() {
	defer1()
	fmt.Println("----------------")
	defer2()
	fmt.Println("----------------")
	defer3()
	fmt.Println("----------------")
	defer4()
	fmt.Println("----------------")
	defer5()

}
